package com.kaka.bus.engine.h5;

import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.kaka.bus.R;
import com.kaka.bus.base.SimpleActivity;
import com.kaka.bus.view.MutipleLayout;

import butterknife.BindView;

/**
 * Created by OnionMac on 2018/1/5.
 */

public class BankCostodyActivity extends SimpleActivity {
    @BindView(R.id.toolbar_back)
    ImageView mToolbarBack;
    @BindView(R.id.toolbar_name)
    TextView mToolbarName;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.pact_progressbar)
    ProgressBar mPactProgressbar;
    @BindView(R.id.pact_webview)
    WebView mPactWebview;
    @BindView(R.id.pact_muti)
    MutipleLayout mPactMuti;

    public static final String URL = "url";
    public static final String TITLE = "title";
    private String mUrl;
    public static int RESULTCODE_BANKCUSTODY = 1002;
    public static int RESULTCODE_REPAY = 1003;
    public static String RESULTCODE = "code";
    private String mTitle;

    @Override
    protected void initView() {
        mToolbarBack.setOnClickListener(v -> finish());
        mPactMuti.setStatus(MutipleLayout.Loading);
        initWebView();

        mPactWebview.addJavascriptInterface(new JSInterface(),"jfq");
    }


    class JSInterface{

        @JavascriptInterface
        public void result(String code){
            Intent intent = new Intent();
            intent.putExtra(RESULTCODE,code);
            setResult(RESULTCODE_BANKCUSTODY,intent);
            finish();
        }

        @JavascriptInterface
        public void callApp(String code){
            Intent intent = new Intent();
            intent.putExtra(RESULTCODE,code);
            setResult(RESULTCODE_REPAY,intent);
            finish();
        }
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        mUrl = intent.getStringExtra(URL);
        mTitle = intent.getStringExtra(TITLE);
        mToolbarName.setText(mTitle);
        mPactMuti.setStatus(MutipleLayout.Success);
        mPactWebview.loadUrl(mUrl);
    }

    private void initWebView() {
        WebSettings settings = mPactWebview.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setDefaultTextEncodingName("UTF-8");
        settings.setSupportZoom(false);
        settings.setPluginState(WebSettings.PluginState.ON);
        mPactWebview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        mPactWebview.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                if (newProgress == 100) {
                    mPactProgressbar.setVisibility(View.GONE);
                } else {
                    mPactProgressbar.setVisibility(View.VISIBLE);
                    mPactProgressbar.setProgress(newProgress);
                }
            }
        });

        mPactWebview.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                return super.shouldOverrideUrlLoading(view,url);
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mPactWebview.canGoBack()) {

            mPactWebview.goBack(); // goBack()表示返回WebView的上一页面
            return true;
        }
        return super.onKeyDown(keyCode,event);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_h5;
    }


}
